<?php
/**
 * Created by 鹰商通
 * User: tulihua
 * Date: 2015/5/11
 * Time: 14:54
 */

namespace Common\Org\Pay\wxpay\lib;
use Common\Org\Pay\wxpay\unit\Log;



class WxPayNotifyScan extends WxPayNotify {
    //重写回调处理函数
    public function NotifyProcess($data, &$msg)
    {
        Log::DEBUG("call back:" . json_encode($data));

        if(!array_key_exists("product_id", $data)){
            $msg = "输入参数不正确";
            $this->SetReturn_code("FAIL");
            return false;
        }

        $len = strlen($data['product_id']);
        $lastStr = substr($data['product_id'],-2);
        $type = substr($lastStr,0,1);
        $orderType = substr($lastStr,1,1);
        $order_id = substr($data['product_id'],0,$len-2);
        //$order_id = $data['product_id'];
        $openid = $data['openid'];

        if($type == 1){
            $detail = D('Common/RechargeOrder')->getOrderDetail(array('id'=>$order_id));
            $money = $detail['money'];
            $body = '出国风_账号充值';
        }else if($type == 3){
            $detail = D('Common/OnlineOrder')->detail($order_id);
            $money = $detail['price'];
            $body = '出国风_买家付款';
        }else if($type == 4){
            if(in_array($orderType,array(1,2))){
                $model =  D('Common/OnlineOrder');
            } else {
                $model = D('Common/OfflineOrder');
            }
            $detail = $model->detail($order_id);
            if($detail['status'] == 3){
                $body = '出国风_中介补交服务费';
                $money = bcsub(5000,$detail['price'],2);
            }else if($detail['status'] == 10){
                $body = '出国风_中介支付投资佣金';
                $money = bcdiv(bcmul($detail['invest_money'],1,2),100,2);
                $currencyRate = D('Common/Currency')->getCurrencyRate($detail['invest_currency_id']);
                $money = bcmul($money,$currencyRate,2);
            }
        }else if($type == 5){
            if(in_array($orderType,array(1,2))){
                $model =  D('Common/OnlineOrder');
            } else {
                $model = D('Common/OfflineOrder');
            }
            $detail = $model->detail($order_id);
            $distribute_detail = unserialize($detail['distribute_detail']);
            $body = '出国风_项目商支付分销佣金';
            if($orderType == 2){
                if($detail['status'] == 9){
                    //第一笔佣金
                    $money = $distribute_detail['p_period'] == 2 ? $distribute_detail['p_mul']['platform_1'] : $distribute_detail['platform'];
                } else if($detail['status'] == 13 && $distribute_detail['p_period'] == 2){
                    //第二笔佣金
                    $money = $distribute_detail['p_mul']['platform_2'];
                }
            }else if($orderType == 3){
                if($detail['status'] == 2){
                    //第一笔佣金
                    $money = $distribute_detail['p_period'] == 2 ? $distribute_detail['p_mul']['platform_1'] : $distribute_detail['platform'];
                } else if($detail['status'] == 5 && $distribute_detail['p_period'] == 2){
                    //第二笔佣金
                    $money = $distribute_detail['p_mul']['platform_2'];
                }
                $currencyRate = D('Common/Currency')->getCurrencyRate($distribute_detail['pcurrcy_id']);
                $money = bcmul($money,$currencyRate,2);
            }
        }

        //统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetBody($body);
        $attach = $order_id.':'.$type.':'.$orderType; //订单id:财务类型
        $input->SetAttach($attach);
        $input->SetOut_trade_no( C('MCHID').date("YmdHis"));
        $input->SetTotal_fee($money*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag($body);
        $input->SetNotify_url(C('TMPL_PARSE_STRING.__ADMIN__').'/Pay/wxNotify');
        $input->SetTrade_type("NATIVE");
        $input->SetOpenid($openid);
        $input->SetProduct_id($order_id);

        $result = WxPayApi::unifiedOrder($input);
        if(!array_key_exists("appid", $result) ||
            !array_key_exists("mch_id", $result) ||
            !array_key_exists("prepay_id", $result))
        {
            $msg = "统一下单失败";
            $this->SetReturn_code("FAIL");
            return false;
        }

        $this->SetData("appid", $result["appid"]);
        $this->SetData("mch_id", $result["mch_id"]);
        $this->SetData("nonce_str", WxPayApi::getNonceStr());
        $this->SetData("prepay_id", $result["prepay_id"]);
        $this->SetData("result_code", "SUCCESS");
        $this->SetData("err_code_des", "OK");
        $this->SetReturn_code("SUCCESS");

        return true;
    }

} 